package com.mvp.shq.washer.ui.recharge;

import com.mvp.shq.washer.entity.RechargeBean;
import com.mvp.shq.washer.entity.WxPayBean;
import com.mvp.shq.washer.entity.result.AlipayResult;
import com.mvp.shq.washer.entity.result.BaseResult;
import com.mvp.shq.washer.entity.RechargeMoney;
import com.mvp.shq.washer.entity.RechargeResponse;
import com.mvp.shq.washer.entity.result.WxPayResult;
import com.mvp.shq.washer.http.BaseObserver;
import com.mvp.shq.washer.utils.SpUtil;
import com.mvp.shq.washer.utils.T;

import java.util.List;

/**
 * Created by Administrator on 2017/8/19.
 */

public class RechargePresenter extends RechargeConstract.Presenter {
    @Override
    public void onStart() {
        //getMoney();
        getRechageMoney();
    }

    @Override
    public void getMoney() {
        model.rechargeMoney().subscribe(new BaseObserver<List<RechargeMoney>>(view) {
            @Override
            protected void onSuccees(BaseResult<List<RechargeMoney>> t) throws Exception {
                if (!t.isError()){
                    view.getRecharMoneySuccess(t.getData());
                }else {
                    view.getRecharMoneyFail(t.getMsg());
                }
            }

            @Override
            protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                view.getRecharMoneyFail("获取充值金额失败");
            }
        });
    }

    @Override
    public void getRechageMoney() {
        model.getRechargeMoney().subscribe(new BaseObserver<List<RechargeBean>>(view) {
            @Override
            protected void onSuccees(BaseResult<List<RechargeBean>> t) throws Exception {
                if (!t.isError()){
                    view.getRechargeMoneyS(t.getData());
                }else {
                    view.getRecharMoneyFail(t.getMsg());
                }
            }

            @Override
            protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                view.getRecharMoneyFail("获取充值金额失败");
            }
        });
    }

    @Override
    public void alipay(RechargeResponse response) {
        String token = SpUtil.getToken();
        model.recharge(token,response)
                .subscribe(new BaseObserver<AlipayResult>(view) {
                    @Override
                    protected void onSuccees(BaseResult<AlipayResult> t) throws Exception {
                        if (t.isError()){
                            T.show(t.getMsg());
                        }else {
                            view.alipayS(t.getData());
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        e.printStackTrace();
                        T.show("未知错误");
                    }
                });
    }

    @Override
    public void wxPay(RechargeResponse response) {
        String token = SpUtil.getToken();
        model.wxRecharge(token,response)
                .subscribe(new BaseObserver<WxPayBean>(view) {
                    @Override
                    protected void onSuccees(BaseResult<WxPayBean> t) throws Exception {
                        if (t.isError()){
                            T.show(t.getMsg());
                        }else {
                            view.wxPayS(t.getData());
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        e.printStackTrace();
                        T.show("未知错误");
                    }
                });
    }
}
